package edu.gatech.cs2340.thecyclonesquad.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class MyDBHelper extends SQLiteOpenHelper {
	
	public static final String COLUMN_ID = "_id";
	public static final String COLUMN_USERNAME = "username";
	public static final String COLUMN_PASSWORD = "password";
	public static final String COLUMN_LOGINATTEMPTS = "loginattempts";
	public static final String COLUMN_LOCKED = "locked";
	public static final String COLUMN_ADMIN = "admin";
	public static final String TABLE_NAME = "person";

	public static final String COLUMN_ID2 = "_id";
	public static final String COLUMN_OWNER = "owner";
	public static final String COLUMN_NAME = "name";
	public static final String COLUMN_DESCRIPTION = "description";
	public static final String COLUMN_LOCATION = "location";
	public static final String COLUMN_REWARD = "reward";
	public static final String COLUMN_DATE = "date";
	public static final String COLUMN_TYPE = "type";
	public static final String COLUMN_CATEGORY = "category";
	public static final String COLUMN_STATUS = "status";
	public static final String TABLE_NAME2 = "item";
	
	public static final String ADMIN_USERNAME = "'admin'";
	public static final String ADMIN_PASSWORD = "'admin'";
	public static final String ADMIN_LOGINATTEMPTS = "'0'";
	public static final String ADMIN_LOCKED = "'false'";
	public static final String ADMIN_ADMIN = "'true'";
	
	
	private static final String DATABASE_NAME = "tornado.db";
	private static final int DATABASE_VERSION = 1;
	
	
	
	/**
	 * This is a constructor for this class.
	 * @param context is information about application environment
	 */
	public MyDBHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
			// TODO Auto-generated constructor stub
	}

	/**
	 * This method called when creating DB.
	 */
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE " + TABLE_NAME +  " (" + 
					COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
					COLUMN_USERNAME + " TEXT NOT NULL, " +
					COLUMN_PASSWORD + " TEXT NOT NULL, " +
					COLUMN_LOGINATTEMPTS + " TEXT NOT NULL, " +
					COLUMN_LOCKED + " TEXT NOT NULL, " +
					COLUMN_ADMIN + " TEXT NOT NULL);"
				);
		
		db.execSQL("CREATE TABLE " + TABLE_NAME2 +  " (" + 
				COLUMN_ID2 + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
				COLUMN_OWNER + " TEXT NOT NULL, " +
				COLUMN_NAME + " TEXT NOT NULL, " +
				COLUMN_DESCRIPTION + " TEXT NOT NULL, " +
				COLUMN_LOCATION + " TEXT NOT NULL, " +
				COLUMN_REWARD + " TEXT NOT NULL, " +
				COLUMN_DATE + " TEXT NOT NULL, " +
				COLUMN_TYPE + " TEXT NOT NULL, " +
				COLUMN_CATEGORY + " TEXT NOT NULL, " +
				COLUMN_STATUS + " TEXT NOT NULL);"				
				);
		
		db.execSQL("INSERT INTO " + TABLE_NAME + " ("
				+ COLUMN_USERNAME + " , " +
				COLUMN_PASSWORD + " , " +
				COLUMN_LOGINATTEMPTS + " , " +
				COLUMN_LOCKED + " , " +
				COLUMN_ADMIN + ")" +
				" VALUES (" +
				ADMIN_USERNAME + " , " +
				ADMIN_PASSWORD + " , " +
				ADMIN_LOGINATTEMPTS + " , " +
				ADMIN_LOCKED + " , " +
				ADMIN_ADMIN + " );"
			);

	}
			
	/**
	 * This method called when upgrading DB version.
	 */
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME2);
		onCreate(db);
	}
}
